<?php

namespace app\dao\user;

use app\dao\BaseDao;
use app\model\ShopUser;

class ShopUserDao extends BaseDao
{
    public function getByShopIdAndOpenId($shopId, $openid)
    {
        return $this->getModel()->where(['shop_id'=>$shopId, 'wxmini_openid'=>$openid])->find();
    }

    public function getPageList(array $data)
    {
        $dateTime = app('DateTime');
        $query = $this->getModel();

        if (!empty($data['role']))
        {
            $where['role_id'] = explode(',', $data['role']);
            array_push($where['role_id'], 'or');

            $query = $query->where($where);
        }

        if (!empty($data['level']))
        {
            $where['level_id'] = explode(',', $data['level']);
            array_push($where['level_id'], 'or');

            $query = $query->where($where);
        }

        if ($data['is_vip'] !== '')
        {
            $query = $query->where('is_vip', '=', $data['is_vip'] ? 1 : 0);
        }

        if (!empty($data['search_start_time']))
        {
            $start_time = $dateTime->date2timestamp($data['search_start_time']);
            $query = $query->where('create_time', '>=', $start_time);
        }

        if (!empty($data['search_end_time']))
        {
            $end_time = $dateTime->date2timestamp($data['search_end_time']);
            $query = $query->where('create_time', '<=', $end_time);
        }

        if (!empty($data['tags']))
        {
            $query = $query->where('tags', 'like', '%' . $data['tags'] . '%');
        }

        if (!empty($data['keyword']))
        {
            $query = $query->where('nickname', 'like', '%' . $data['keyword'] . '%');
        }

        $query = $query->with(['level', 'role', 'referrer', 'roleInfo']);

        return $this->pagination($query, $data['page'], $data['perPage']);
    }

    public function getAll(array $data)
    {
        $dateTime = app('DateTime');
        $query = $this->getModel();

        if (!empty($data['role']))
        {
            $where['role_id'] = explode(',', $data['role']);
            array_push($where['role_id'], 'or');

            $query = $query->where($where);
        }

        if (!empty($data['level']))
        {
            $where['level_id'] = explode(',', $data['level']);
            array_push($where['level_id'], 'or');

            $query = $query->where($where);
        }

        if ($data['is_vip'] !== '')
        {
            $query = $query->where('is_vip', '=', $data['is_vip'] ? 1 : 0);
        }

        if (!empty($data['search_start_time']))
        {
            $start_time = $dateTime->date2timestamp($data['search_start_time']);
            $query = $query->where('create_time', '>=', $start_time);
        }

        if (!empty($data['search_end_time']))
        {
            $end_time = $dateTime->date2timestamp($data['search_end_time']);
            $query = $query->where('create_time', '<=', $end_time);
        }

        if (!empty($data['tags']))
        {
            $query = $query->where('tags', 'like', '%' . $data['tags'] . '%');
        }

        if (!empty($data['keyword']))
        {
            $query = $query->where('nickname', 'like', '%' . $data['keyword'] . '%');
        }

        $query = $query->with(['level', 'role', 'referrer', 'roleInfo']);

        return [
            'pagination' => ['page' => 1, 'perPage' => -1, 'total' => $query->count()],
            'list'       => $query->select()
        ];
    }

    public function getById($id)
    {
        return $this->getModel()->with(['level', 'role', 'referrer', 'roleInfo'])->find($id);
    }

    public function updateById($id, $updateList)
    {
        $this->getModel()->where('id', '=', $id)->update($updateList);
    }

    protected function setModel(): string
    {
        return ShopUser::class;
    }

}